<?php

namespace Application;

abstract class ModelAbstract
{
    private $db;
    protected $table;

    public function __construct()
    {
        $this->db = Db::getInstance();
        return $this;
    }

    public function getDb()
    {
        return $this->db;
    }

    public function getList($order, $desc = null)
    {
        $select = $this->getDb()->select();
        $select->from($this->table);
        if ($order) {
            if (null !== $desc) {
                $order .= ' DESC';
            }
            $select->order($order);
        }
        return $select->query()->fetchAll();
    }

    public function find(array $key)
    {
        reset($key);
        $column = key($key);
        $select = $this->getDb()->select();
        $select->from($this->table);
        $select->where($column . ' = ?', $key[$column]);
        //$table = new \Zend_Db_Table();
        //$table->find($key);
        return $this->getDb()->fetchRow($select);
    }

    public function delete($id)
    {
        $where = $this->getDb()->quoteInto('id IN (?)', $id);
        return $this->getDb()->delete($this->table, $where);
    }
}